frappe.pages['wxwork_bind'].on_page_load = function (wrapper) {
	var page = frappe.ui.make_app_page({
		parent: wrapper,
		title: '绑定企业微信',
		single_column: true
	});
	wrapper.wxwork_en = new wxwork_en.wxwork(wrapper, page);
}

if (!window.wxwork_en) { window.wxwork_en = {} };
wxwork_en.wxwork = class wxwork_controller {
	constructor(wrapper, page) {
		this.page_id = wrapper.id;
		this.page = page;
		this.make_form();
	}

	make_form() {
		let $btn = this.page.set_secondary_action('返回主页', () => {
			frappe.set_route("app", "home");
		})

		var wxwork_user = frappe.get_cookies()['wxwork_user'];
		if (wxwork_user) {
			// 添加保存按钮
			let bind_btn = this.page.set_primary_action('绑定', () => this.show_dialog())

			let d = new frappe.ui.Dialog({
				title: '绑定企业微信用户',
				fields: [
					{
						fieldname: "wxwork_user",
						label: "企业微信用户ID",
						fieldtype: "Data",
						default: wxwork_user,
						read_only:true,
						change: function () {
						},
					},
					{
						fieldname: "en_user",
						label: "erpnex用户ID",
						fieldtype: "Data",
						default: frappe.user.full_name(),
						read_only:true,
						change: function () {
						},
					}
				],
				size: 'small', // small, large, extra-large 
				primary_action_label: '提交',
				primary_action(values) {
					console.log(values);
					frappe.call({
						method: 'wxwork_en.wxwork_for_erpnext.wxwork_service.bind_user',
						args: values,
						// disable the button until the request is completed
						btn: $('.primary-action'),
						// freeze the screen until the request is completed
						freeze: true,
						callback: (r) => {							
						},
						error: (r) => {							
						}
					});	
					d.hide();				
				}
			});
			this.d = d;
			d.show();
		}
		else {
			$("<div>没有找到当前登录的企业微信用户,无法绑定!</div>").appendTo(this.page.main);
		};

	}

	show_dialog() {
		this.d.show();
	}	

	do_bind() {		
	}
}
